/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.cmcc.zysoft.infocollection.dao.MenuPcDao;
import com.cmcc.zysoft.infocollection.model.Menu;
import com.cmcc.zysoft.spring.security.model.User;
import com.cmcc.zysoft.spring.security.util.SecurityContextUtil;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: MenuPcService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-6 上午10:11:07
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class MenuPcService extends BaseServiceImpl<Menu, String> {

	@Resource
	private MenuPcDao menuPcDao;
	
	@Override
	public HibernateBaseDao<Menu, String> getHibernateBaseDao() {
		return this.menuPcDao;
	}
	
	/**
	 * 获取菜单树.
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> menuTree() {
		List<Map<String, Object>> list = this.menuPcDao.menuTree("0");
		return this.childMenu(list);
	}
	
	/**
	 * 递归获取子菜单.
	 * @param list 父菜单列表
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> childMenu(List<Map<String, Object>> list) {
		for(Map<String, Object> map : list) {
			String isLeaf = map.get("isLeaf").toString();
			if("N".equals(isLeaf)) {
				String parentId = map.get("id").toString();
				List<Map<String, Object>> childList = this.menuPcDao.menuTree(parentId);
				childMenu(childList);
				map.put("children", childList);
				map.put("state", "closed");
			} else {
				map.put("state", "open");
			}
		}
		return list;
	}
	
	/**
	 * 初始化顶部菜单.
	 * @param companyId 企业ID
	 * @return List<Menu>
	 */
	public List<Menu> initTopMenu() {
		//顶层菜单的父菜单id=0
		final String parentId = "0";
		return this.getMenusByPid(parentId);
	}
	
	/**
	 * 根据父菜单ID获取子菜单列表.
	 * @param pid 父菜单ID
	 * @return List<Menu>
	 */
	public List<Menu> getMenusByPid(final String pid) {
		//获取当前登录用户信息
		User user = SecurityContextUtil.getCurrentUser();
		return this.menuPcDao.getMenusByPid(pid, user);
	}
	
	/**
	 * 根据菜单名称搜索菜单.
	 * @param menuName 菜单名称
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> searchMenu(String menuName) {
		Map<String, Object> map = null;
		List<Map<String, Object>> list = this.menuPcDao.searchMenu(menuName);
		List<Map<String, Object>> parentList = new ArrayList<Map<String, Object>>();
		if(null != list && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				map = list.get(i);
				if(!"0".equals(map.get("parent_id").toString())) {
					parentList.addAll(this.searchParent(map.get("parent_id").toString()));
				}
			}
			list.addAll(parentList);
		}
		return list;
	}
	
	/**
	 * 根据父菜单ID递归找出所有子菜单.
	 * @param menuId 父菜单ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> searchParent(String menuId) {
		List<Map<String, Object>> parentList = this.menuPcDao.searchMenuByMenuId(menuId);
		Map<String, Object> parentMap = parentList.get(0);
		if(!"0".equals(parentMap.get("parent_id").toString())) {
			parentList.addAll(searchParent(parentMap.get("parent_id").toString()));
		}
		return parentList;
	}
	
	/**
	 * 根据菜单ID删除菜单,若该菜单是叶子,则对其父菜单进行相应处理.
	 * @param menuId 菜单ID
	 */
	public void deleteMenu(String menuId) {
		Menu menu = this.menuPcDao.get(menuId);
		this.menuPcDao.delete(menu);
		if("Y".equals(menu.getIsLeaf())) { //要删除的菜单是叶子菜单
			List<Menu> list = this.menuPcDao.findByNamedParam(new String[]{"parentId", "status"}, new Object[]{menu.getParentId(), "1"});
			if(null == list || list.isEmpty()) { //删除该菜单后,其父菜单不存在子菜单,则设置父菜单为叶子节点
				Menu parentMenu = this.menuPcDao.get(menu.getParentId());
				parentMenu.setIsLeaf("Y");
				this.menuPcDao.update(parentMenu);
			}
		}
	}
}